package me.xplabs.model.combatunit 
{
	import adobe.utils.CustomActions;
	import flash.geom.Point;
	import me.xplabs.battle.buff.Buff;
	import me.xplabs.constant.BuffEffectType;
	import me.xplabs.constant.BuffStatus;
	import me.xplabs.Globals;
	import me.xplabs.model.msg.MsgDelBuffActor;
	import me.xplabs.model.msg.MsgHitActor;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.MacroObjPool;
	/**
	 * 角色的基本信息
	 * ...
	 * @author xiaohan
	 */
	public class MRoleInfo extends BaseRole
	{
		//起点坐标
		public var startPos:Point;
		//终点坐标
		public var endPos:Point;
		//唯一id
		public var soleId:int;
		//当前所在的x坐标
		public var curPosX:Number;
		//当前所在的y坐标
		public var curPosY:Number;
		//路径节点列表
		private var _paths:Array;
		//当前节点位置
		public var position:int;
		//当前生命值
		public var curLife:Number;
		//角度
		public var angle:Number;
		
		public var prevCellPos:Point;
		public var curCellPos:Point;
		
		public var cellPos:Function;
		//被击
		public var byHit:int;
		public var buffs:Vector.<Buff>;
		
		//速度修正率
		public var speedOffset:Number;
		public function MRoleInfo() 
		{
			startPos = new Point();
			endPos = new Point();
			buffs = new Vector.<Buff>();
			prevCellPos = new Point( -1, -1);
			curCellPos = new Point( -1, -1);
			speedOffset = 1;
		}
		public function setStartPos(p_x:Number, p_y:Number):void
		{
			startPos.x = p_x;
			startPos.y = p_y;
		}
		public function setEndPos(p_x:Number, p_y:Number):void
		{
			endPos.x = p_x;
			endPos.y = p_y;
		}
		public function setCurPos(p_x:Number, p_y:Number):void
		{
			curPosX = p_x;
			curPosY = p_y;
			
			var len:int = buffs.length;
			for (var i:int = 0; i < len; i++) 
			{
				buffs[i].curPosX = curPosX;
				buffs[i].curPosY = curPosY;
			}
		}
		public function move():Boolean
		{
			if (!nextPathNode) return true;
			curCellPos.x = nextPathNode[0];
			curCellPos.y = nextPathNode[1];
			if (cellPos != null) cellPos(this);
			prevCellPos.x = nextPathNode[0];
			prevCellPos.y = nextPathNode[1];
			var dx:Number = nextPathNode[0] * Globals.cellW - curPosX;
			var dy:Number = nextPathNode[1] * Globals.cellH - curPosY;
			var tempAngle:Number = Math.atan2(dy, dx);
			angle = tempAngle * 180 / Math.PI;
			var tempSpeed:Number = speed * speedOffset;
			//trace(tempSpeed, speed);
			var vx:Number = Math.cos(tempAngle) * tempSpeed;
			var vy:Number = Math.sin(tempAngle) * tempSpeed;
			setCurPos(curPosX + vx, curPosY + vy);
			if (Math.sqrt(dx * dx + dy * dy) > tempSpeed) return false;
			setCurPos(nextPathNode[0] * Globals.cellW, nextPathNode[1] * Globals.cellH);
			position++;
			return false;
		}
		public function get curPathNode():Array
		{
			if (!_paths || position > _paths.length - 1) return null;
			return _paths[position];
		}
		public function get nextPathNode():Array
		{
			if (!_paths || position + 1 > _paths.length - 1) return null;
			return _paths[position + 1];
		}

		/**
		 * 设置角色的寻路路径列表
		 */
		public function set paths(value:Array):void 
		{
			_paths = value;
			position = 0;
			if (_paths.length > 0) setCurPos(_paths[0][0] * Globals.cellW, _paths[0][1] * Globals.cellH);
		}
		public function dispose():void 
		{
			if (_paths) _paths.length = 0;
			cellPos = null;
			soleId = 0;
			prevCellPos.x = -1;
			prevCellPos.y = -1;
			curCellPos.y = -1;
			curCellPos.y = -1;
			startPos.x = 0;
			startPos.y = 0;
			endPos.x = 0;
			endPos.y = 0;
			curPosX = 0;
			curPosY = 0;
			speedOffset = 1;
			byHit = 0;
			if (buffs)
			{
				var len:int = buffs.length;
				for (var i:int = len - 1; i >= 0; i--) 
				{
					delBuff(buffs.splice(i, 1)[0]);
				}
			}
		}
		
		public function addBuff(buff:Buff):void
		{
			//print("buff的长度=" + buffs.length);
			if (buff.gap == 0)
			{
				switch(buff.type)
				{
					case BuffEffectType.POISON:
						
						break;
					case BuffEffectType.SLOW_DOWN:
						speedOffset = buff.effect * 0.01;
						break;
				}
			}
			buff.finishCall = buffTimeOverHandler;
			buff.hitCall = buffHitHandler;
			buff.curPosX = curPosX;
			buff.curPosY = curPosY;
			buffs.push(buff);
			buff.startTime();
			sendMsg(NotificationName.FIGHT_NOTIFICATION, buff, NotificationFight.ADD_BUFF);
		}
		
		private function buffHitHandler(buff:Buff):void 
		{
			//print("角色id" + soleId + "触发buff效果");
			switch(buff.type)
			{
				case BuffEffectType.POISON:
					sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgHitActor(soleId, buff.effect), NotificationFight.HIT_ACTOR);
					break;
			}
		}
		
		private function buffTimeOverHandler(buff:Buff):void 
		{
			//print("buff结束了");
			sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgDelBuffActor(soleId, buff.baseId), NotificationFight.DEL_BUFF);
		}
		
		
		public function removeBuffBySoleId(p_soleId:int, buffType:int):void
		{
			switch(buffType)
			{
				case BuffEffectType.POISON:
					
					break;
				case BuffEffectType.SLOW_DOWN:
					speedOffset = 1;
					break;
			}
			var len:int = buffs.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (p_soleId == buffs[i].soleId)
				{
					delBuff(buffs.splice(i, 1)[0]);
					break;
				}
			}
			
		}
		
		public function removeBuffByBaseId(p_baseId:int):void 
		{
			var len:int = buffs.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (p_baseId == buffs[i].baseId)
				{
					delBuff(buffs.splice(i, 1)[0]);
					break;
				}
			}
		}
		
		private function delBuff(buff:Buff):void
		{
			switch(buff.type)
			{
				case BuffEffectType.POISON:
					
					break;
				case BuffEffectType.SLOW_DOWN:
					speedOffset = 1;
					break;
			}
			sendMsg(NotificationName.FIGHT_NOTIFICATION, buff.soleId, NotificationFight.DEL_BUFF);
			buff.dispose();
			MacroObjPool.dispose(buff);
			buff = null;
		}
		
		
		public function heartbeat(tempGapTime:uint):void 
		{
			//var len:int = buffs.length;
			//for (var i:int = 0; i < len; i++) 
			//{
				//buffs[i].heartbeat(tempGapTime);
			//}
		}
		
		public function checkBuff(buffBaseId:int, baseLevel:int):int
		{
			var len:int = buffs.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (buffs[i].baseId == buffBaseId)
				{
					if (buffs[i].level < baseLevel)
					{
						return BuffStatus.REPLACE_BUFF;
					}
					return BuffStatus.SAME_BUFF;
				}
			}
			return BuffStatus.NO_BUFF;
		}
		
	}
}